﻿using System;
using DomainExample.DomainModel.ContactModel;

namespace DomainExample.Repository.Tests.ContactRepositoryTests
{
    public class ContactRepositoryTests : BaseRepository
    {
        protected readonly IContactRepository Target;

        protected ContactRepositoryTests() : base(new SqlDataProvider(new ConfigConnectionStringProvider()))
        {
            var repositoryFactory = RepositoryFactory.NewRepositoryFactory();
            Target = repositoryFactory.GetContactRepository();
        }

        protected Contact AddTestContact(DateTime date, ContactType contactType, DirectionType direction, string summary)
        {
            var testContact = Contact.NewContact();
            testContact.DateUtc = date;
            testContact.Type = contactType;
            testContact.Direction = direction;
            testContact.Summary = summary;

            return Target.Persist(testContact);
        }

        protected void CreateSchema()
        {
            var command = NewCommand(
                @"CREATE TABLE [dbo].[Contact](
	                [Id] [bigint] IDENTITY(1,1) NOT NULL,
	                [DateUtc] [datetime2](7) NOT NULL,
	                [Type] [nvarchar](max) NOT NULL,
	                [Direction] [nvarchar](max) NOT NULL,
	                [Summary] [nvarchar](max) NOT NULL,
                    CONSTRAINT [PK_Contact] PRIMARY KEY CLUSTERED ( [Id] ASC )
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
                ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");

            ExecuteNonQuery(command);
        }

        protected void DestroySchema()
        {
            var command = NewCommand("DROP TABLE dbo.Contact");
            ExecuteNonQuery(command);
        }
    }
}
